
do "E:\yungyu\lottery_baby\do\set_environment.do"

capture log close
log using "$log\3.1.define_populaion_`c(current_date)'.log" , replace
clear
set more off

forv i = 96(1)101{ //Lottery Year{
	
	loc j = `i' - 1

	use "$public\observation\real_jhq_v2.dta" , clear

	gen year = `i'
	keep if born_year <= `i' & death_year >= `i' // Alive
	gen age = `i' - born_year
	keep if inrange(age,20,44)

	** Marriage record
	gen married = 0

	replace year = `i' - 1
	rename idn pers_idn 
	merge m:1 year pers_idn using "$public\couple\couple_`j'.dta"
	drop if _m == 2
	replace married = 1 if couple_idn~=""
	rename couple_idn spouse_idn
	drop _m

	** Prize record
	forv k = 0(1)3{
		dis "merge lottery prize information of `k' year prior to baseline year..."
		replace year = `i' - `k'
		merge 1:1 year pers_idn using "$wdata/all_winner_3types_nonsprt_93109.dta"
		quiet drop if _m == 2
		drop _m
		rename payment_t_amt pre`k'prize
	}
	rename pre0prize prize

	** Fertility record
	rename pers_idn idn
	forv k = 1(1)3{
		dis "merge fertility information of `k' year prior to baseline year..."
		replace year = `i' - `k'
		loc j = `i' - `k' 
		merge m:1 idn year using "$wdata\childnum_`j'.dta", update noreport
		quiet drop if _m==2
		drop _m prebabyboy prebabygirl 
		foreach y in babyboy babygirl totbabyboy totbabygirl{
			quiet replace `y' = 0 if `y' == .
			rename `y' `y'_pre`k'
		}
		quiet gen baby_pre`k' = babyboy_pre`k' + babygirl_pre`k'
		quiet gen totbaby_pre`k' = totbabyboy_pre`k' + totbabygirl_pre`k'
		quiet gen pre`k'born = baby_pre`k' > 0
	}
	rename idn pers_idn

	save "$wdata/temp.dta", replace

	** Income and wealth record
	foreach x in "pers" "spouse" {
		
		if ("`x'" == "spouse") rename pers_idn self_idn
		
		replace year = `i' - 1
		loc j = `i' - 1

		rename `x'_idn idn
		
		** Income
		dis "merge `x' income information..."
		cap noi merge m:1 year idn using "$wdata/income_`j'.dta" , update noreport //As couple idn with some missing, cannot uniquely identify, using m:1 to merge
		cap drop if _m==2
		cap drop _m income_*
		** Earnings
		dis "merge `x' earnings information..."
		merge m:1 year idn using "$earnings/earnings_`j'.dta" , update noreport  //As couple idn with some missing, cannot uniquely identify, using m:1 to merge
		
		quiet drop if _m==2
		if `j' < 104{
			quiet replace wage = wage + est_pay if year == `j'
			drop est
		}
		drop _m
		lab var busi "business income"
		lab var prof "professional income"
		lab var wage "wage income"
		lab var earnings "earnings"

		** Wealth
		rename idn pers_idn
		dis "merge `x' wealth information..."
		merge m:1 year pers_idn using "$wealth/ind_wealth_`j'.dta", update replace noreport keepusing(tot_mrkt_net_t21 rl_est_mrkt houseloan_v4 captl_tot_saving value_stk_2 bu_ast_1)
		quiet drop if _m==2
		drop _m
		
		quiet{
			gen asset = tot_mrkt_net_t21	
			gen rlest = rl_est
			gen hloan = houseloan_v4
			gen saving = captl_tot_saving
			gen stock = value_stk_2 + bu_ast_1
			replace bu_ast_1 = 0 if bu_ast_1 < 0
			gen grossstock = value_stk_2 + bu_ast_1
			gen grossasset = rl_est_mrkt + captl_tot_saving + value_stk_2 + bu_ast_1
			gen liquidasset = grossstock + saving
		}
		drop tot_mrkt_net* rl_est_mrkt houseloan_v4 captl_tot_saving value_stk_2 bu_ast_1
		 
		foreach y in "income" "busi" "prof" "wage" "earnings" "asset" "rlest" "hloan" "saving" "stock" "grossstock" "grossasset" "liquidasset"{
			rename `y' `x'_`y'
			dis "set `y' missing to zero"
			recode `x'_`y' . = 0
		}
		rename pers_idn `x'_idn
		
		if ("`x'" == "spouse") rename self_idn pers_idn 
	}

	** Adjusted for CPI
	cap drop _merge
	merge m:1 year using "$wdata/cpi.dta", noreport
	drop if _m == 2
	drop cpi_sy _merge
	foreach x in "pers" "spouse" {
		foreach y in "income" "busi" "prof" "wage" "earnings"{
			dis "adjust `x's' `y' by cpi..."
			replace `x'_`y' = `x'_`y'/cpi*100
		}
	}

	** Residence
	replace year = `i'

	foreach x in "pers" "spouse" {
		if ("`x'" == "spouse") rename pers_idn self_idn  
		rename `x'_idn pers_idn
		dis "merge `x's' resident information..."
		merge m:1 year pers_idn using "$public/residence/resident_`i'.dta", update noreport keepusing(hsn_cd town_cd)
		quiet drop if _m == 2
		drop _m
		
		rename hsn_cd `x'_hsn
		rename town_cd `x'_town
		rename pers_idn `x'_idn
		if ("`x'" == "spouse") rename self_idn pers_idn   
	}

	quiet gen houhld_resid = pers_hsn

	drop if houhld_resid == ""
	encode houhld_resid, gen(houhld_resid_cd)

	** Adjust prize amount
	foreach x in prize pre1prize pre2prize pre3prize{
		replace `x' = `x' / 1000
		replace `x' = `x' / cpi*100
		replace `x' = `x' * 0.8
		recode `x' . = 0
	}
	gen winyr_pers_real_prize_aftertax = prize

	** Adjust income/earning/wealth/prize to thousand
	foreach x of varlist pers_asset-pers_liquidasset spouse_asset-spouse_liquidasset{
		replace `x' = `x' * 1000
	}

	gen resident_tpe = houhld_resid == "A"
	gen resident_munici = houhld_resid == "B" | houhld_resid == "D" | houhld_resid == "E" | houhld_resid == "F" | houhld_resid == "H" | houhld_resid == "L" | houhld_resid == "R" | houhld_resid == "S"
	gen resident_other = resident_tpe == 0 & resident_munici == 0
	gen resident_sixcities = resident_tpe == 1 | resident_munici == 1 

	compress

	replace year = `i' - 1
	gen win_year = `i'

	save "$wdata\population_`i'.dta" , replace
}

log close
